#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu();
void year_calendar();
void year_month_calendar();
void year_month_day_calendar();
int main(void) {
	int choice;
	while(1) {
		menu();
		printf("请选择你要的日历服务序号：");
		scanf("%d", &choice);
		switch(choice) {
			case 1:
				year_calendar();
				break;
			case 2:
				year_month_calendar();
				break;
			case 3:
				year_month_day_calendar();
				break;
		}
	}
	return 0;
}

void menu() {
	printf("\n");
	printf("_________________________________________________________________\n");
	printf("                       欢迎来到日历查询系统                      \n");
	printf("                          1：查询某年日历                         \n");
	printf("                          2：查询某年某月的日历                   \n");
	printf("                          3：查询某年月日的日历信息               \n");
	printf("_________________________________________________________________\n");
	printf("\n");
	system("color B0");
}

void year_calendar() {
	int i, j, h, k, r, m, n, s, year, mon[12] = {31,28,31,30,31,30,31,31,30,31,30,31}, day[12];
	printf("请输入年份：");
	scanf("%d", &year);
	printf("想要排几列：");
	scanf("%d", &n);
	if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		mon[1] = 29;
	day[0] = (year + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7;
	for (m = 1; m < 12; m++)
		day[m] = (day[m-1] + mon[m-1]) % 7;
	for (i = 1; i <= n; i++) {
		printf("_____________________   ");
	}
	printf("\n");
	for(i = 1; i <= 12 / n + 1; i++) {
		for(j = 1; j <= 10; j++) {
			for (r = 1, s = (i - 1) * n; r <= n && s < 12; r++, s++) {
				if (j == 1)
					printf("%8d年%3d月      ", year, s+1);
				if (j == 2||j == 10)
					printf("_____________________");
				if (j == 3)
					printf(" 日 一 二 三 四 五 六");
				for (k = (j - 4) * 7 + 1, h = 1; k <= 42 && h <= 7; k++, h++) {
					if (k <= mon[s] + day[s]) {
						if (j == 4) {
							if (k <= day[s])
								printf("   ");
							else
								printf("%3d", k - day[s]);
						} else if (j != 1 && j !=2 && j != 3 && j != 10)
							printf("%3d", k - day[s]);
					} else
						printf("   ");
				}
				printf("   ");
			}
			printf("\n");
		}
	}


}

void year_month_calendar() {
	int a, b;
	int year, month, day, monthmax;
	printf("请输入年份：");
	scanf("%d", &year);
	printf("请输入月份：");
	scanf("%d", &month);
	if(month == 1 || month == 3 || month == 5|| month == 7 || month == 8 || month == 10 || month == 12) monthmax = 31;
	if(month == 2) {
		if(year % 4 == 0 || (year % 100 == 0 && year % 400 == 0)) monthmax = 29;
		else monthmax = 28;
	}
	if(month == 4 || month == 6 || month == 9 || month == 11) monthmax = 30;
	if(month == 1 || month == 2) {
		month += 12;
		year--;
	}
	day = ( 2 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
	if(day == 0) {
		day = 7;
	}
	printf("________________________________________________________\n");
	printf("一	二	三	四	五	六	日\n");
	printf("________________________________________________________\n");
	for(a = 1; a <= day - 1; a++) {
		printf("	");
	}
	for(b = 1; b <= monthmax; b++) {
		printf(" %d	", b);
		if((b + day -1) % 7 == 0)
			printf("\n");
	}
}

void year_month_day_calendar() {
	int year, month, dayy, day, day1, day2, weekday, flag, n1, n2, n, i;
	int month_day[13];
	struct tm *local;
	time_t t;
	printf("\n请输入查询的年月日（空格隔开）：");
	scanf("%d%d%d", &year, &month, &dayy);
	printf("输入的日期是：%d年%d月%d日\n", year, month, dayy);
	if( month == 1|| month == 2) {
		month += 12;
		year--;
	}
	/*int iWeek = (dayy + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;*/
	int iWeek;
	iWeek =  (dayy + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
	switch(iWeek) {
		case 0:
			printf("输入的日期是：星期一\n");
			break;
		case 1:
			printf("输入的日期是：星期二\n");
			break;
		case 2:
			printf("输入的日期是：星期三\n");
			break;
		case 3:
			printf("输入的日期是：星期四\n");
			break;
		case 4:
			printf("输入的日期是：星期五\n");
			break;
		case 5:
			printf("输入的日期是：星期六\n");
			break;
		case 6:
			printf("输入的日期是：星期日\n");
			break;
	}
	if((month == 1 && dayy == 1) || (month == 3 && dayy == 8) ||(month == 3 && dayy == 12) ||(month == 4 && dayy == 5) ||(month == 5 && dayy == 1) ||(month == 5 && dayy == 4) || (month == 6 && dayy == 1) ||(month ==7 && dayy == 1) ||(month == 8 && dayy == 1) ||(month == 9 && dayy == 3) ||(month == 9 && dayy == 10) ||(month == 10 && dayy == 1))
		printf("输入的日期是公历节日\n");
	else printf("输入的日期不是公历节日\n");
	day = 0;
	for(i = 1; i <= year - 1; i++) {
		if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
			n = 366;
		else n = 365;
		day = day + n;
	}
	day1 = day;
	month_day[0] = 0;
	for(i = 1; i <= 12; i++) {
		switch(i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				month_day[i] = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				month_day[i] = 30;
				break;
			case 2:
				if(year % 400 == 0 || (year % 4 == 0 && year% 100 != 0) )
					month_day[i] = 29;
				else month_day[i] = 28;
				break;
		}
	}
	for(i = 1,n1 = 0; i <= month - 1; i++)
		day1 = day1 + month_day[i];
	day1 = day1 + dayy;
	n1 = day1;
	t = time(NULL);
	local = localtime(&t);
	year = local->tm_year + 1900;
	month = local->tm_mon + 1;
	dayy = local->tm_mday;
	printf("今天的日期是：%d年%d月%d日\n", year, month, dayy);
	day2 = day;
	for(i = 1; i <= 12; i++) {
		switch(i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				month_day[i] = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				month_day[i] = 30;
				break;
			case 2:
				if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
					month_day[i] = 29;
				else month_day[i] = 28;
				break;
		}
	}
	for(i = 1, n2=0; i <= month - 1; i++)
		day2 = day2 + month_day[i];
	day2 = day2 + dayy;
	n2 = day2;
	printf("输入日期距离当前系统日期还有：%d天\n",n2-n1);
}